﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Text;
using System.Web.Script.Serialization;

//测试使用，新增客服
namespace WeChat.Common
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SetKefu1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string json = setNewKefu();
            //string json = "{\"kf_account\" : \"test@sz_Jocat\",\"nickname\" : \"客服1\",\"password\" : \"123456\"}";
            ApplicationLog.Write("发送的json请求：" + json);
            string accesstoken = GetAccess_token.getAccesstoken();
            string url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" + accesstoken;
            GetReturnJson gt = new GetReturnJson();
            //Dictionary<string, object> dic = gt.postReturnJson(url,json);
            var js = new JavaScriptSerializer();
            var dic = js.Deserialize<Dictionary<string, object>>(gt.postReturnJson(url, json));

            //Dictionary<string, object> dic = GetReturnJson.CreatePostHttpResponse(url, json, null, null, null, null);
            if (dic["errcode"].ToString().Equals("0"))
            {
                ApplicationLog.Write("新增客服成功！");
            }
            else
            {
                ApplicationLog.Write("新增客服失败！errcode：" + dic["errcode"]);
            }
        }

        public string setNewKefu()
        {
            StringWriter sw = new StringWriter();
            JsonWriter writer = new JsonTextWriter(sw);

            writer.WriteStartObject();
            //客服账号
            writer.WritePropertyName("kf_account");
            writer.WriteValue("test2@sz_Jocat");
            //客服昵称
            writer.WritePropertyName("nickname");
            writer.WriteValue("客服2");
            //客服密码 
            writer.WritePropertyName("password");
            string pwd = "123456";
#pragma warning disable CS0618 // “FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)”已过时:“The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.”
            pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5").ToString();
#pragma warning restore CS0618 // “FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)”已过时:“The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.”
            writer.WriteValue(pwd);
            writer.WriteEndObject();
            writer.Flush();

            string jsonText = sw.GetStringBuilder().ToString();
            return jsonText;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
